(function (global, factory) { global = global; global.cp.CmCalendar = factory(); }(this, function () { 'use strict'; var Component = (function (isUndefined) { var win = window, $ = win.jQuery, Util = win.EB.util; function Component (container, args) { if (!(this instanceof Component)) { return new Component(container, args); } var defParams = { container: container, cdWrap: '[data-calendarlayer__wrap]', restDays: [ ], customEvent: '.Component' + (new Date()).getTime() + Math.random() }; this.opts = Util.def(defParams, (args || {})); if (!(this.obj = $(container)).length) return; this.init(); } Component.prototype = { init: function () { this.setElements(); this.buildParams(); this.buildDatepicker(); this.buildLayer(); this.obj.data('CmCalendar', this); }, setElements: function () { this.$cdWrap = this.obj.find(this.opts.cdWrap); }, buildParams: function () { var restDays = this.opts.restDays; Util.def(this, { params: { oRest: {}, aStr: ['00','01','02','03','04','05','06','07','08','09'], build: function () { var p = this; restDays.forEach(function (y) { var year = y.year; y.detail.forEach(function (m) { var month = m.month.length < 2 ? p.aStr[m.month] : m.month; m.day.forEach(function (d) { var day = d.length < 2 ? p.aStr[d] : d; var fullDate = year + '-' + month + '-' + day; if (!p.oRest.hasOwnProperty(fullDate)) { p.oRest[fullDate] = ''; } }); }); }); } } }); this.params.build(); }, buildDatepicker: function () { var _this = this; Util.def(this, { datepicker: { opts: { restDays: _this.opts.restDays, isInvalidDate: function (date) { var df = date.format('YYYY-MM-DD'); if (_this.params.oRest.hasOwnProperty(df)) { return true; } }, }, instance: null, destroy: function () { if (this.instance == null) return; this.instance.destroy(); this.instance = null; }, applyDatepicker: function (e, picker) { _this.obj.trigger('changeCmCalendar.cmcalendar', picker.startDate.format('YYYY-MM-DD')); }, hideDatepicker: function () { _this.layer.close(); _this.obj.trigger('hideCmCalendar.cmcalendar'); }, showCalendar: function () { _this.obj.trigger('showCmCalendar.cmcalendar'); }, priceClick: function (e) { e.stopPropagation(); var $t = $(e.currentTarget); $t.closest('td.available').trigger('mousedown.daterangepicker'); }, build: function () { if (this.instance != null) return this.instance = new UIDatepicker(_this.$cdWrap, this.opts); win.setTimeout(function () { this.instance.datepicker.instance.container.on(_this.changeEvents('mousedown'), '.available em', this.priceClick.bind(this)); }.bind(this), 0); this.instance.inputEl.on('apply.daterangepicker', this.applyDatepicker.bind(this)); this.instance.inputEl.on('hide.daterangepicker', this.hideDatepicker.bind(this)); this.instance.inputEl.on('showCalendar.daterangepicker', this.showCalendar.bind(this)); this.instance.inputEl.trigger('focusin'); } } }); }, buildLayer: function () { var _this = this; Util.def(this, { layer: { instance: null, destroy: function () { if (this.instance == null) return; _this.obj.off(_this.changeEvents('layerOpenStart')); _this.obj.off(_this.changeEvents('layerCloseAfter')); this.instance.destroy(); this.instance = null; }, close: function () { if (this.instance == null) return; _this.obj.trigger('closeLayer'); }, build: function () { if (this.instance != null) return; if (_this.obj.data('CmLayer') == isUndefined) { this.instance = new CmLayer(_this.obj, { effect: 'default' }); } else { this.instance = _this.obj.data('CmLayer'); } _this.obj.on(_this.changeEvents('layerOpenStart'), function () { _this.datepicker.build(); }); _this.obj.on(_this.changeEvents('layerCloseAfter'), function () { _this.datepicker.destroy(); }); } } }); this.layer.build(); }, changeEvents: function (event) { var events = [], eventNames = event.split(' '); for (var key in eventNames) { events.push(eventNames[key] + this.opts.customEvent); } return events.join(' '); }, destroy: function () { this.layer.destroy(); this.datepicker.destroy(); this.obj.removeData('CmCalendar'); } }; return Component; })(); return Component; }));